﻿using RndMaterialDisposalService.Models;
using RndMaterialDisposalService.Models.RequestModel;
using RndMaterialDisposalService.Services.Interface;
using Microsoft.AspNetCore.Mvc;
using Razor.Templating.Core;
using System.Text.Json;

namespace RndMaterialDisposalService.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class EmailTemplateController : ControllerBase
    {

        private readonly ISMOService service;
        private readonly IConfiguration configuration;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_service"></param>
        public EmailTemplateController(ISMOService _service, IConfiguration _configuration)
        {
            service = _service;
            configuration = _configuration;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost("ReadRazorTemplateToHtml")]
        public async Task<IActionResult> ReadRazorTemplateToHtml()
        {
            var model = new EmailTemplates.TransferView.TransferModel()
            {
                Name = "barry",
                Message = "this is razor view page",
                Email = "BarryHu@k2workflow.com.cn",
                Priority = "11111111"
            };
            // 读取Razor视图页面为html字符串
            var html = await RazorTemplateEngine.RenderAsync("~/Views/EmailTemplates/TransferView/Transfer.cshtml", model);
            return Ok(html);
        }

        /// <summary>
        /// 临时工艺文件下发单电子流(邮件模板)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("TemporaryProcessDocumentEmailTemplate")]
        public async Task<IActionResult> TemporaryProcessDocumentEmailTemplate(TemporaryProcessDocumentElectronFlowReq model) 
        {
            var html = await RazorTemplateEngine.RenderAsync("~/Pages/EmailTemplates/TemporaryProcessDocument/Index.cshtml", model);
            //if (!string.IsNullOrWhiteSpace(model.viteFormUrl)) 
            //{
            //    html = html.Replace("\r\n", "");
            //    html = html.Replace("\\", "");
            //    html = html.Replace("[ViteFormUrl]", model.viteFormUrl);
            //}
            return Ok(html);
        }

        /// <summary>
        /// 小批量试用跟踪电子流(邮件模板)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("SmallBatchTrialEmailTemplate")]
        public async Task<IActionResult> SmallBatchTrialEmailTemplate(SmallBatchTrialElectronFlowReq model)
        {
            var html = await RazorTemplateEngine.RenderAsync("~/Pages/EmailTemplates/SmallBatchTrial/Index.cshtml", model);

            return Ok(html);
        }

        /// <summary>
        /// 外协加工(邮件模板)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("OutsourcingProcessingEmailTemplate")]
        public async Task<IActionResult> OutsourcingProcessingEmailTemplate(OutsourcingProcessingElectronFlowReq model)
        {
            var html = await RazorTemplateEngine.RenderAsync("~/Pages/EmailTemplates/OutsourcingProcessing/Index.cshtml", model);

            return Ok(html);
        }

        /// <summary>
        /// 中试验证问题处理电子流(邮件模板)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("ValidationProblemEmailTemplate")]
        public async Task<IActionResult> ValidationProblemEmailTemplate(ValidationProblemFlowReq model)
        {
            var approverSendToEmail = configuration.GetSection("ApproverSendToEmail").Value;
            var html = await RazorTemplateEngine.RenderAsync("~/Pages/EmailTemplates/ValidationProblem/Index.cshtml", model);
            html = html.Replace("[{ApproverSendToEmail}]", approverSendToEmail);
            return Ok(html);
        }

    }
}
